import { getServiceEnvConfig } from '@/config/env'
import { setNavbarHeihgt } from '@/store'
import { useSystemInfo } from '@/store/system'

export const getImgPath = (filename?: string) => {
  const { assetsUrl } = getServiceEnvConfig()
  return `${assetsUrl}${filename}`
}
/**
 * 获取系统信息 设置导航栏高度
 */
export const getSystemInfo = () => {
  const { systemInfo } = storeToRefs(useSystemInfo())
  return new Promise<UniApp.GetSystemInfoResult>((_resolve) => {
    uni.getSystemInfo({
      success: (e) => {
        console.log(e, 'getSystemInfo')
        systemInfo.value = e
        // 获取手机状态栏高度
        const statusBar = e.statusBarHeight || 0
        let customBar

        // #ifndef MP
        customBar = statusBar + (e.platform === 'android' ? 50 : 45)
        // #endif

        // #ifdef MP-WEIXIN
        // 获取胶囊按钮的布局位置信息
        const menu = uni.getMenuButtonBoundingClientRect()
        // 导航栏高度 = 胶囊下距离 + 胶囊上距离 - 状态栏高度
        customBar = menu.bottom + menu.top - statusBar
        // #endif

        // #ifdef MP-ALIPAY
        customBar = statusBar + (e.titleBarHeight || 0)
        // #endif

        setNavbarHeihgt({
          statusBarH: statusBar,
          customBarH: customBar,
        })
        _resolve(e)
      },
      fail: (e) => {
        console.log(e, 'getSystemInfo')
      },
    })
  })
}
